package com.example.anydo.activity;
import com.example.anydo.R;
import com.example.anydo.adapter.MusicAdapter;
import com.example.anydo.constant.Music;
import com.example.anydo.util.database.DatabaseHelper;
import com.example.anydo.util.pub_util.MusicPlayer;
import com.example.anydo.util.pub_util.SPUtil;
import com.example.anydo.util.pub_util.Util;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener {

    private ArrayList<String> musicName = new ArrayList<>();
    private RecyclerView rv_music;
    private MusicAdapter myAdapter;
    private TextView tv_date;
    private TextView tv_name;
    private DatabaseHelper dbHelper;
    private TextView confirm;
    private ImageView cancel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

        dbHelper = new DatabaseHelper(this);

        initView();

    }

    private void initView() {

        tv_name = findViewById(R.id.tv_name);
        tv_date = findViewById(R.id.tv_date);
        confirm = findViewById(R.id.confirm);
        cancel = findViewById(R.id.cancel);
        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);

        tv_name.setText(dbHelper.getUserNameById(
                Integer.parseInt(SPUtil.getUserId(this)
                )
        ));
        tv_date.setText(Util.getDayNowShow());

        for (String name: Music.music) {
            musicName.add(name);
        }
        //创建一个simpleAdapter
        myAdapter = new MusicAdapter(musicName,this,
                (int)SPUtil.getData(this,SPUtil.KEY_MUSIC,0));
        rv_music = findViewById(R.id.rv_music);

        rv_music.setAdapter(myAdapter);

        rv_music.setLayoutManager(new LinearLayoutManager(this));

        rv_music.addItemDecoration(new DividerItemDecoration(this, LinearLayout.VERTICAL));

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.confirm:
                SPUtil.putData(this,SPUtil.KEY_MUSIC,myAdapter.getPosition());
                myAdapter.onDestroy();
                finish();
                break;
            case R.id.cancel:
                myAdapter.onDestroy();
                finish();
                break;
        }
    }
}